import util from "../../utils/util"
import api from "../../utils/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:"",
    phonenumber:"",
    passworld:"",
    repeatpass:"",
    identifycode:"",
    isCanSubmit:false,
    identifyText:"获取验证码",
    imageidenfify:"",
    confirmText:"",
    imageidentifyUrl:"",
    role:2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getImageIdentifyCode();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /**
   * 提交注册信息
   */
  submit:function(){
    var that = this;
    if (!this.data.isCanSubmit){
        return;
    }
    //校验
    var flag = util.validate({
      username: this.data.username,
      phonenumber: this.data.phonenumber,
      passworld: this.data.passworld,
      identifycode: this.data.identifycode,
      repeatpass:this.data.repeatpass
    });
    if(flag){
      this.setData({
        confirmText: flag
      });
        return;
    }
    // 提交信息
    wx.request({
      url: api.register,
      success:function(response){
        console.log("注册成功！");
        console.log(response.data);
        that.setData({
          confirmText:response.data.infor
        });
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data:{
          userName:that.data.username,
          pwd:that.data.passworld,
          confirmPwd:that.data.repeatpass,
          phone:that.data.phonenumber,
          sendSMS: that.data.identifycode,
          verCode: that.data.imageidenfify,
          hy:that.data.role
      },
      method:"POST"
    })
   
   
  },
  /**
   * 输入时自动同步
   */
  inputUsername: function (e) {
    this.setData({
      username: e.detail.value
    });
    this.canSubmit();
  },
  inputRepeatpass: function (e) {
    this.setData({
      repeatpass: e.detail.value
    });
    this.canSubmit();
  },
  inputImageidenfify: function (e) {
    this.setData({
      imageidenfify: e.detail.value
    });
    this.canSubmit();
  },
  inputPhone:function(e) {
    this.setData({
      phonenumber:e.detail.value
    });
    this.canSubmit();
  },
  inputPassWorld:function (e) {
    this.setData({
      passworld:e.detail.value
    });
    this.canSubmit();
  },
  inputIdentifyCode:function(e){
    this.setData({
      identifycode:e.detail.value
    });
    this.canSubmit();
  },
  /**
   * 判断提交按钮是不是显示
   */
  canSubmit:function(){
    if(this.data.phonenumber.trim() && this.data.passworld.trim()&&this.data.identifycode.trim()&&this.data.repeatpass&&this.data.imageidenfify&&this.data.username){
        //可以提交
        this.setData({
          isCanSubmit:true
        });
    }else{
      this.setData({
        isCanSubmit:false
      });
    }
  },
  /**
   * 点击获取验证码
   */
  getIdentifyCode:function(){
    console.log("成功发送验证码");
    //发送验证码
    util.sendIdentifyCode(this.data.phonenumber);
    //倒计时
    var seconds = 10;
    var that = this;
    var timer;
    if (this.data.identifyText == "获取验证码"){
       timer = setInterval(function () {
        seconds--;
        that.setData({
          identifyText: seconds
        });
        if (that.data.identifyText == "0"){
          clearInterval(timer);
          that.setData({
            identifyText:"获取验证码"
          });
        }
      }, 1000);
    }
    
  },
  /**
   * 点击获取图片验证码
   */
  getImageIdentifyCode:function(){
    var that = this;
      wx.request({
        url: api.getImageIdentify,
        success:function(response){
          console.log(response.data);
          that.setData({
            imageidentifyUrl:api.URLHeader + response.data.info
          });
        }
      })
  },
  checkRole:function (e) {
    this.setData({
      role:e.detail.value
    });
  }

})